﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DrSoftsFrame.Servers;

namespace PreeRegisterDAL
{
    public class WorkUpdateDAL
    {
        /// <summary>
        /// 获取排班号源更新信息
        /// </summary>
        /// <returns></returns>
        public static DataSet GetWorkUpdateInfo(bool IFTmkyy,string strwhere)
        {
            try
            {
                string sqlstr = string.Format(@"declare @nMoney money

if exists(select * from OPDSystemParameter with(nolock)  where cParameterCode='cParameterNumber')
select @nMoney=cParameterNumber from OPDSystemParameter where cParameterCode='cParameterNumber'
else
set @nMoney=0.5


select a.iPersonID,a.iWorkPlanID,a.iDeptID,e.DeptName,convert(varchar(10),a.dStartTime,120) dDate,nMaxPreengageNum=isnull(a.nMaxPreengageNum,0)
,a.nMaxDiagnoseNum
,leftpreenum=isnull(a.nMaxPreengageNum,0)-isnull(a.npreengageNum,0),a.cTimeTypeCode
,stime=convert(varchar(5),a.dStartTime,108)
,etime=convert(varchar(5),a.dEndTime,108)
,a.cDiagnoseTypeCode,b.cDiagnoseTypeName
,registermoney=isnull(@nMoney,0),b.nDiagnoseRate,c.nAddRate,d.cLocation
,isnull(a.bStopFlag,0) bStopFlag,a.cHosCode,a.preenbegin
into #opdworkplan 
from opdworkplan a
left join opdDiagnoseType b on a.cDiagnoseTypeCode=b.cDiagnoseTypeCode
left join opddictdept c on a.iDeptID=c.iDeptID
left join dictdept e on a.iDeptID=e.No_Dept
left join SSDiagnoseHouse d ON d.iHouseID = a.ihouseid  
where {0}

select * from #opdworkplan;", strwhere);

                // 增加是否同煤版参数  liujia150811
                if (IFTmkyy)
                {
                    sqlstr += string.Format(@"select a.iDetailId,a.iWorkPlanID,a.iNum
                                                ,stime=convert(varchar(5),a.dStartTime,108)+'-'+convert(varchar(5),a.dEndTime,108)
                                                ,istatus=
                                                case 
                                                when isnull(a.bclinicflag,0)=1 OR isnull(a.bdiagflag,0)=1 or isnull(a.buseflag,0)=1 then 1 
                                                else 0 
                                                end 
                                                from opdworkplandetail a
                                                join #opdworkplan b on a.iWorkPlanID=b.iWorkPlanID
                                                where a.iNum>isnull(b.preenbegin,0) 
                                                and (CONVERT(varchar(100),a.dStartTime, 8)>'07:00:00' and CONVERT(varchar(100),a.dStartTime, 8)<'12:00:00'
                                                or  CONVERT(varchar(100),a.dStartTime, 8)>'14:00:00' and CONVERT(varchar(100),a.dStartTime, 8)<'18:00:00')");
                }
                else
                {
                    sqlstr += string.Format(@"select iDetailId,iWorkPlanID,iNum
                                                ,stime=convert(varchar(5),dStartTime,108)+'-'+convert(varchar(5),dEndTime,108)
                                                ,istatus=
                                                case 
                                                when isnull(bPreRegister,0)=1 OR isnull(bCheck,0)=1 or isnull(bLock,0)=1 then 1 
                                                else 0 
                                                end 
                                                from opdworkplandetail
                                                where iWorkPlanID in 
                                                (
                                                   select iWorkPlanID from #opdworkplan
                                                )");
                }

                ClinicDb mydb = new ClinicDb();
                DataSet ds = mydb.ExecuteSelect(sqlstr);
                if (ds == null || ds.Tables.Count < 1)
                {
                    return null;
                }
                ds.Tables[0].TableName = "opdworkplan";
                ds.Tables[1].TableName = "opdworkplandetail";
                return ds;

            }
            catch (Exception err)
            {

                throw;
            }
        }
    }
}
